
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import java.io.*;


public class SearchParty
{
    /** 
     * This method is called on the event dispatching thread, after
     * the startApp worker thread has finished initializing the GUI (see
     * the SearchGUI.init method).  Create the main app frame and add
     * the gui's contentPane to it and then align the applications
     * splashPane (a descendant of the frame) over the splashWindow.
     * Show the frame and hide the splash screen.
     */
    private void showApp(final SearchGUI gui, JWindow splash)
    {
	JFrame frame = new JFrame(gui.getTitle());
	// TBD f.setIconImage(gui.getIconImage());

	WindowListener l = new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
		gui.quit();
	    }
	};
	frame.addWindowListener(l); 

	frame.getRootPane().setJMenuBar(gui.getMenuBar());
	frame.getContentPane().add(gui.getContentPane());
	frame.pack();

	Point splashPaneOffset = new Point();
	SwingUtilities.convertPointToScreen(splashPaneOffset, gui.getSplashPane());
	int frameX = splash.getX() - splashPaneOffset.x;
	int frameY = splash.getY() - splashPaneOffset.y;
	frame.setLocation(frameX, frameY);

	frame.setVisible(true);
	splash.setVisible(false);
    }


    /**
     * Start the SearchParty application: get the splash screen up and
     * then start a worker thread that will initialize the rest of the 
     * GUI and then show the application frame with showApp().
     * This method runs on the main thread.
     */
    private void startApp() 
    {
	System.getProperties().put("proxyHost", "webcache-cup");
	System.getProperties().put("proxyPort", "8080");

	final SearchGUI gui = new SearchGUI();

	/* Put up a splash "screen" window in the center of the screen 
	 * and display the gui's splashIcon in it.
	 */
	final JWindow splash = new JWindow();
	JComponent splashPane = new JLabel(gui.getSplashIcon());
	splash.getContentPane().add(splashPane);
	splash.pack();
	Dimension splashSize = splash.getSize();
	Dimension screenSize = splash.getToolkit().getScreenSize();
	int splashX = (screenSize.width / 2) - (splashSize.width / 2);
	int splashY = (screenSize.height / 2) - (splashSize.height / 2);
	splash.setLocation(splashX, splashY);
	splash.setVisible(true);

	/* Start a worker thread that will finish initializing the GUI
	 * and put up the main application frame with showApp().  
	 */
	final Runnable doShowApp = new Runnable() {
	    public void run() {  // on the event dispatching thread
		showApp(gui, splash);  
	    }
	};

	Runnable doInitGUI = new Runnable() {
	    public void run() {  // on a worker thread
		gui.init();
		EventQueue.invokeLater(doShowApp);
	    }
	};

	new Thread(doInitGUI, "Initialize SearchParty GUI").start();
    }
       

    public static void main(String[] args) {
	new SearchParty().startApp();
    }
}

